package dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class conexion {
private Connection conexion;
static String bd="agenda";
static String user="root";
static String password="pablo";
static String server="jdbc:mysql://localhost/"+bd;
 
/** Creates a new instance of BasedeDatos */
public conexion() {
}
 
public void establecerConexion(){
   try{
    Class.forName("com.mysql.jdbc.Driver");
    conexion = DriverManager.getConnection(server,user,password);
   }catch(Exception e){
   System.out.println("Imposible realizar conexion con la BD");
   e.printStackTrace();
   }
 }
 
public  Connection getConexion(){
     return conexion;
}
 
public void cerrar(ResultSet rs){
   if(rs !=null){
    try{
       rs.close();
    }catch(Exception e){
    System.out.print("No es posible cerrar la Conexion");
    }
   }
}
 
public void cerrar(java.sql.Statement stmt){
   if(stmt !=null){
    try{
     stmt.close();
    }catch(Exception e){}
   }
}
 
 public void destruir(){
   if(conexion !=null){
   try{
      conexion.close();
   }catch(Exception e){}
   }
 }
 
 public ResultSet retorna_sql(String COMANDO_SQL){
    Connection cm;
    Statement st;
    establecerConexion();
    cm=getConexion();
    ResultSet resultado;
    try{
        st=cm.createStatement();
        resultado=st.executeQuery(COMANDO_SQL);
        return resultado;
    }catch(Exception ex){
        System.out.println("error..."+ex.toString());
        return null;
    }
}
public boolean ejecuta_sql(String COMAND_SQL){
    Connection cm;
    Statement st;
    try{
         establecerConexion();
        cm=getConexion();
        st=cm.createStatement();
        st.executeUpdate(COMAND_SQL);
        cm.close();
        st.close();
        return true;
    }catch(Exception ex){
        System.out.println("Errot..." +ex.toString());
        return false;
    }
}

public ResultSet seleccion (String query) throws SQLException
	{	
                establecerConexion();
                Connection var= getConexion();
		Statement stmt = var.createStatement();
                ResultSet resultado=stmt.executeQuery(query);
                return resultado;
	}
 
 
}
